package com.zzb.util;

import java.util.Random;


public class StringUtil {
	public static String optString(String string) {
		return optString(string, "");
	}

	public static String optHTMLString(String string) {
		return optString(string, "&nbsp;");
	}

	/**
	 * 获取非空字符串，空字串由指定字符串代替
	 * 
	 * @param string
	 *            原字符串
	 * @param string2
	 *            string为空时的替换字符串
	 * @return
	 */
	public static String optString(String string, String string2) {
		return isEmpty(string) ? string2 : string;
	}

	public static String getSubString(String string, int index) {
		if (string != null && string.length() > index) {
			return string.substring(0, index) + "..";
		} else {
			return optString(string);
		}
	}

	public static String getSubHTMLString(String string, int index) {
		if (string != null && string.length() > index) {
			return "<span title='" + string + "'>" + string.substring(0, index)
					+ "..</span>";
		} else {
			return optHTMLString(string);
		}
	}

	public static boolean isEmpty(String string) {
		if (string == null || string.equals("")
				|| string.toLowerCase().equals("null")
				|| string.toLowerCase().equals("undefined"))
			return true;
		else
			return false;
	}
	/**
	 *  随机生成16位字符串
	 * @return
	 */
	public static String getRandomStr() {
		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 16; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
}
